package cuongnh.project.japanesediary.vocabulary.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="tbl_text")
public class Text extends AbstractModelObject{

	@Id
	@Column(name = "text_id")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="text_seq")
	@SequenceGenerator(
			name="text_seq",
			sequenceName="text_sequence",
			allocationSize=1
		)
	private Long textId;
	
	@Column(name="text", length=500)
	private String text;
	
	@Column(name="text_vietnamese", length=500)
	private String vnText;
	
	@Column(name="text_english", length=500)
	private String enText;
	
	@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_text_vocabulary", joinColumns = { @JoinColumn(name = "text_id") }, inverseJoinColumns = { @JoinColumn(name = "vocabulary_id") })
	private Set<Vocabulary> vocabularies = new HashSet<Vocabulary>();
	
	@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_text_kanji", joinColumns = { @JoinColumn(name = "text_id") }, inverseJoinColumns = { @JoinColumn(name = "kanji_id") })
	private Set<Kanji> kanjis = new HashSet<Kanji>();
	
	@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_text_grammar", joinColumns = { @JoinColumn(name = "text_id") }, inverseJoinColumns = { @JoinColumn(name = "grammar_id") })
	private Set<Grammar> grammars = new HashSet<Grammar>();
	
	@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_context_text", joinColumns = { @JoinColumn(name = "text_id") }, inverseJoinColumns = { @JoinColumn(name = "context_id") })
	private Set<Context> contexts = new HashSet<Context>();
	
	@Column(name="text_audio_path", length=500)
	private String audioFilePath;
	
	@Column(name = "created_date")
	private Date createdDate;

	@Column(name = "modified_date")
	private Date modifiedDate;
	
	public Long getTextId() {
		return textId;
	}

	public void setTextId(Long textId) {
		this.textId = textId;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getVnText() {
		return vnText;
	}

	public void setVnText(String vnText) {
		this.vnText = vnText;
	}

	public String getEnText() {
		return enText;
	}

	public void setEnText(String enText) {
		this.enText = enText;
	}
	
	public Set<Vocabulary> getVocabularies() {
		return vocabularies;

	}

	public void setVocabularies(Set<Vocabulary> vocabularies) {
		Set<Vocabulary> oldValue = this.vocabularies;
		this.vocabularies = vocabularies;
		firePropertyChange("vocabularies", oldValue, vocabularies);
	}
	
	public Set<Kanji> getKanjis() {
		return kanjis;
	}

	public void setKanjis(Set<Kanji> kanjis) {
		Set<Kanji> oldValue = this.kanjis;
		this.kanjis = kanjis;
		firePropertyChange("kanjis", oldValue, kanjis);
	}
	
	public Set<Grammar> getGrammars() {
		return grammars;
	}

	public void setGrammars(Set<Grammar> grammars) {
		Set<Grammar> oldValue = this.grammars;
		this.grammars = grammars;
		firePropertyChange("grammars", oldValue, grammars);
	}

	public String getAudioFilePath() {
		return audioFilePath;
	}

	public void setAudioFilePath(String audioFilePath) {
		this.audioFilePath = audioFilePath;
	}
	
	
	public Set<Context> getContexts() {
		return contexts;
	}

	public void setContexts(Set<Context> contexts) {
		Set<Context> oldValue = this.contexts;
		this.contexts = contexts;
		firePropertyChange("contexts", oldValue,
				contexts);
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean sameSame = false;

		if (obj != null && obj instanceof Vocabulary) {
			sameSame = textId.equals(((Text) obj).textId);
		}

		return sameSame;
	}

	@Override
	public int hashCode() {
		return this.textId.intValue();
	}
}
